package com.blue.excel.type;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by ChenYaHui on 2017/8/8.
 */
public class CellTypeHelper {

    public static CellType getCellType(Object object){
        if(isNum(object)){
            return CellType.NUMBER;
        }
        Class<?> clasz = object.getClass();
        if(String.class == clasz){
            String strValue = (String) object;
            if(isPercentum(strValue)){
                return CellType.PERCENTUM;
            }else{
                return CellType.STRING;
            }
        }else if(Date.class == clasz){
            return CellType.DATE;
        }else if(Boolean.class == clasz || boolean.class == clasz){
            return CellType.BOOLEAN;
        }

        return CellType.Auto;
    }


    public static boolean isNum(Object object) {
        try {
            new BigDecimal(String.valueOf(object));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isPercentum(String str){
        if(str.endsWith("%")){
            String strValue_1 = str.replaceAll(",", "");
            String strValue_2 = strValue_1.substring(0, strValue_1.length() - 1);
            if(isNum(strValue_2)){
                return true;
            }
            else{
                return false;
            }
        }

        return false;
    }
}
